package com.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.BasePageReq;
import com.base.BasePageRes;
import com.base.BaseServiceImpl;
import com.entity.VipVoucher;
import com.exception.ServiceException;
import com.repository.VipVoucherRepository;
import com.req.VipVoucherReq;
import com.res.VipVoucherRes;
import com.service.VipVoucherService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class VipVoucherServiceImpl extends BaseServiceImpl<VipVoucherReq,VipVoucherRes, VipVoucherRepository, VipVoucher> implements VipVoucherService {
    @Override
    public VipVoucherRes saveVipUser(VipVoucherReq req) {
        return this.handle(req);
    }

    @Override
    public BasePageRes queryPage(BasePageReq req) {
        if (StringUtils.isEmpty(req.getPage())) {
            throw new ServiceException("请传入page");
        }
        if (StringUtils.isEmpty(req.getSize())) {
            throw new ServiceException("请传入size");
        }


        QueryWrapper<VipVoucher> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(VipVoucher::getCrDate);
        if (!StringUtils.isEmpty(req.getDes())) {
            wrapper.lambda().like(VipVoucher::getCrId, req.getDes()).
                    or().like(VipVoucher::getLevelId, req.getDes())
            ;


        }

        IPage<VipVoucher> pages = new Page<>(req.getPage(), req.getSize());
        IPage<VipVoucher> articalIPage = this.page(pages, wrapper);
        BasePageRes pageRes = this.convertIpage(articalIPage, new VipVoucherRes());
        return pageRes;
    }
}
